/*
 |--------------------------------------------------------------------------
 | 网络拦截器
 |--------------------------------------------------------------------------
 |
 |
 */


const axios = require('axios')
const stringify = require('qs').stringify
const axiosConfig = require('./axios.config')
const status = require('./status.code')
// const baseURL = require('../config').appDomain
// 创建axios实例
const service = axios.create(axiosConfig);
// service.defaults.baseURL = baseURL
// service.defaults.baseURL = 'http://jinrapp.pre.jinr.com'
// service.defaults.baseURL = 'http://jinrapp.beta.jinr.com'
// service.defaults.baseURL = "http://jinrapp.jinr.com"

// request拦截器
let requestConfig
service.interceptors.request.use(config => {
  requestConfig = config
  //unescape(config.data)
  // console.log('--config---', config)
  return config;
}, error => {
  // Do something with request error
  console.log(error); // for debug
  Promise.reject(error);
})
// respone拦截器
service.interceptors.response.use((response) => {

  console.log(`
  \x1B[33m ------------> ${requestConfig.method} 请求主站START ------------------------------------
    \n \u001b[34m url=>\u001b[39m ${requestConfig.url}
    \n \u001b[34m 请求参数=>\u001b[39m ${unescape(requestConfig.data)}
    \n \u001b[34m <--- ${requestConfig.method}请求主站app返回值==> \u001b[39m ${response.data}
    \n \x1B[33m -----------------END-------------------------------------------
  `)
  return response.data

}, (error) => {
  console.log(`
    \u001b[34m --------> ${requestConfig.method} 请求主站
    \n \u001b[34m url=>\u001b[39m ${requestConfig.url}
    \n \u001b[34m 请求参数=>\u001b[39m ${unescape(requestConfig.data)}
    \n \x1B[32m <-------- ${requestConfig.method}请求主站app返回值error==> \u001b[39m ${error}
  `)
  console.log('err' + error); // for debug
  return Promise.reject(error);
})

module.exports = service;